package main.java.com.blog.controller;

import com.blog.entity.User;
import com.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Admin on 2017/5/31.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private static int j = 0;
    private static Boolean b = false;
    private static String fileName = null;
    private static String userName = null;
    @Autowired
    UserService userService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String insert(String userAccount, String userEmail, String userPassword) {

        System.out.println(userAccount + userEmail + userPassword);
        User user = new User();
        user.setUserAccount(userAccount);
        user.setUserEmail(userEmail);
        user.setUserPassword(userPassword);

        userService.addUser(user);

        return "redirect:../resources/login.jsp";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model, String userAccount, String userPassword) {
        userName = userAccount;
        List<User> userList = userService.findAll();

        for (int i = 0; i < userList.size(); i++) {
            if (userList.get(i).getUserAccount().equals(userAccount) &&
                    userList.get(i).getUserPassword().equals(userPassword)) {
                j = i;
                b = true;
                break;
            } else if (userList.get(i).getUserAccount().equals(userAccount) &&
                    !userList.get(i).getUserPassword().equals(userPassword)) {
                j = i;
                b = false;
                break;
            } else {
                j = userList.size();
            }

        }
        if (b == true && j != userList.size()) {
            model.addAttribute("user", userList.get(j));
            return "User";
        } else if (j != userList.size() && b == false) {
            return "redirect:../resources/login.jsp";
        } else {
            return "redirect:../resources/sign.jsp";
        }

    }

    @RequestMapping(value = "/fileOnload", method = RequestMethod.POST)
    public String fileOnload(@RequestParam("attach") MultipartFile file, HttpServletRequest req) throws IOException {

        String path = req.getSession().getServletContext().getRealPath("") + "../../../src/main/webapp/resources/img";
        System.out.println(file.getOriginalFilename()
                + ";" + file.getContentType()
                + ";" + file.getName());

        fileName = file.getOriginalFilename();

        File dir = new File(path, fileName);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        file.transferTo(dir);

        User user = new User();
        user.setUserAccount(userName);
        user.setUserPhono(fileName);
        userService.updateByUserAccount(user);

        return "/fileOnload";
    }

    @ResponseBody
    @RequestMapping(value = "/addImg", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String addImg(String userAccount) {
        User user = userService.findByAccount(userAccount);
        String imgSrc = user.getUserPhono();

        return imgSrc;
    }

}
